import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/login'
import isCheack from '@/isCheack'
import App from '@/layout/index'
import Storage from '@/services/storage'
import store from '@/store'

const router = new VueRouter({
  mode: 'hash',
  base: '/ecshop/req/',
  routes: [
    {
      path: '/login',
      name: 'login',
      component: Login
    },
    {
      path: '/isCheack',
      name: 'isCheack',
      component: isCheack
    },
    {
      path: '/',
      component: App,
      meta: {auth: true, breadcrumbList: '首页'},
      children: [
        {
          name: 'app',
          path: '/',
          component: () => System.import('@/client/home')
        },
        {
          path: 'system',
          component: () => System.import('@/client/default-link'),
          meta: {breadcrumbList: '系统管理'},
          children: [
            {
              path: 'roles',
              name: 'roles',
              meta: {breadcrumbList: '角色管理'},
              component: () => System.import('@/client/roles')
            },
            {
              path: 'users',
              name: 'users',
              meta: {breadcrumbList: '操作员管理'},
              component: () => System.import('@/client/users')
            },
            {
              path: 'operation',
              name: 'operation',
              meta: {breadcrumbList: '日志管理'},
              component: () => System.import('@/client/operation')
            }
          ]
        },
        {
          path: 'Backstage',
          component: () => System.import('@/client/default-link'),
          meta: {breadcrumbList: '后台业务管理'},
          children: [
            {
              path: 'product',
              name: 'product',
              meta: {breadcrumbList: '商品管理'},
              component: () => System.import('@/client/product')
            },
            {
              path: 'product/proBanner',
              name: 'proBanner',
              meta: {breadcrumbList: '机构轮播图'},
              component: () => System.import('@/client/product/proBanner')
            },
            {
              path: 'institution',
              name: 'institution',
              meta: {breadcrumbList: '机构管理'},
              component: () => System.import('@/client/institution')
            },
            {
              path: 'institution/insProduct',
              name: 'insProduct',
              meta: {breadcrumbList: '机构商品'},
              component: () => System.import('@/client/institution/insProduct')
            },
            {
              path: 'institution/insBanner',
              name: 'insBanner',
              meta: {breadcrumbList: '机构轮播图'},
              component: () => System.import('@/client/institution/insBanner')
            },
            {
              path: 'Merchant',
              name: 'Merchant',
              meta: {breadcrumbList: '商户管理'},
              component: () => System.import('@/client/Merchant')
            },
            {
              path: 'order',
              name: 'order',
              meta: {breadcrumbList: '订单管理'},
              component: () => System.import('@/client/order')
            },
            {
              path: 'coupons',
              name: 'coupons',
              meta: {breadcrumbList: '电子券管理'},
              component: () => System.import('@/client/coupons')
            },
            {
              path: 'coupons/accredit',
              name: 'accredit',
              meta: {breadcrumbList: '电子券发券'},
              component: () => System.import('@/client/coupons/accredit')
            },
            {
              path: 'coupons/relation',
              name: 'relation',
              meta: {breadcrumbList: '电子券关联'},
              component: () => System.import('@/client/coupons/relation')
            },
            {
              path: 'business',
              name: 'business',
              meta: {breadcrumbList: '交易管理'},
              component: () => System.import('@/client/business')
            },{
              path: 'business/insInfo',
              name: 'insInfo',
              meta: {breadcrumbList: '交易信息设置'},
              component: () => System.import('@/client/business/insInfo')
            },
          ]
        },
        {
          path: 'Reports',
          component: () => System.import('@/client/default-link'),
          meta: {breadcrumbList: '报表管理'},
          children: [
            {
              path: 'Goodsreport',
              name: 'Goodsreport',
              meta: {breadcrumbList: '销售商品报表'},
              component: () => System.import('@/client/Goodsreport')
            },
            {
              path: 'Amtreport',
              name: 'Amtreport',
              meta: {breadcrumbList: '销售金额报表'},
              component: () => System.import('@/client/Amtreport')
            },
            {
              path: 'Allamtreport',
              name: 'Allamtreport',
              meta: {breadcrumbList: '销售金额汇总表'},
              component: () => System.import('@/client/Allamtreport')
            },
            {
              path: 'Allgoodsreport',
              name: 'Allgoodsreport',
              meta: {breadcrumbList: '销售商品汇总表'},
              component: () => System.import('@/client/Allgoodsreport')
            },
            {
              path: 'couponsReport',
              name: 'couponsReport',
              meta: {breadcrumbList: '优惠券报表'},
              component: () => System.import('@/client/couponsReport')
            }
          ]
        },{
          path: 'index',
          component: () => System.import('@/client/default-link'),
          meta: {breadcrumbList: '首页管理'},
          children: [
            {
              path: 'institutionBanner',
              name: 'institutionBanner',
              meta: {breadcrumbList: '轮播图管理'},
              component: () => System.import('@/client/institutionBanner')
            },{
              path: 'module',
              name: 'module',
              meta: {breadcrumbList: '模块管理'},
              component: () => System.import('@/client/module')
            },{
              path: 'institutionBanner/Banner',
              name: 'Banner',
              meta: {breadcrumbList: '机构轮播图管理'},
              component: () => System.import('@/client/institutionBanner/insBanner')
            },{
              path: 'module/insModule',
              name: 'insModule',
              meta: {breadcrumbList: '模块设置'},
              component: () => System.import('@/client/module/insModule')
            },{
              path: 'module/insAdvert',
              name: 'insAdvert',
              meta: {breadcrumbList: '广告设置'},
              component: () => System.import('@/client/module/insAdvert')
            }
          ]
        },,{
          path: 'category',
          component: () => System.import('@/client/default-link'),
          meta: {breadcrumbList: '分类管理'},
          children: [
            {
              path: 'productCategory',
              name: 'productCategory',
              meta: {breadcrumbList: '商品分类'},
              component: () => System.import('@/client/productCategory')
            },
          ]
        },
        {
          name: 'not-found',
          path: '*',
          component: () => System.import('@/client/not-found')
        }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.auth)) { // meta路由元信息
    if (!Storage.token) {
      next({
        name: 'login',
        query: {redirect: to.fullPath}
      })
    } else {
      next()
    }
  } else {
    next()
  }
})

router.afterEach((to, from) => {
  let list = []
  to.matched.forEach(item => {
    let value = {
      meta: item.meta,
      path: item.path
    }
    list.push(value)
  })
  if (to.matched.some(record => record.meta.auth)) {
    store.commit('set-modules', list)
  }
})
Vue.use(VueRouter)

export default router
